/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// ptouchdev.hpp - Pure virtual base class for touch devices
//
// Copyright (c) 1997-2008 Swell Software, Inc.
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software, Inc.
// is strictly prohibited.
//
// Notes:
//
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#if defined(PEG_TOUCH_SUPPORT)

#ifndef PTOUCHDEV_HPP_
#define PTOUCHDEV_HPP_

#ifndef PEG_TOUCH_NOISE_LIMIT
#define PEG_TOUCH_NOISE_LIMIT  50   // some variation in consecutive ATOD
#endif                              // readings is normal. A lot of variation
                                    // indicates the screen is settling and
                                    // the user is just touching or releasing.
                                    // How much variation between reads is OK
                                    // is determined by this variable

#ifndef PEG_TOUCH_SAMPLES
#define PEG_TOUCH_SAMPLES      8    // how many samples do we average for one
#endif                              // reading?

#ifndef PEG_TOUCH_POLL_DELAY
#define PEG_TOUCH_POLL_DELAY   2    // how long does touch task suspend
#endif                              // between check for movement?

#ifndef PEG_TOUCH_PROMPT_DELAY
#define PEG_TOUCH_PROMPT_DELAY 100  // timer ticks to delay while prompting
#endif

/*--------------------------------------------------------------------------*/
// TOUCH_STATE
//
// Calibration data and status for touch driver
/*--------------------------------------------------------------------------*/
typedef struct
{
    PEGUSHORT RawXVals[4];
    PEGUSHORT RawYVals[4];
    PEGINT    ScreenWidth;
    PEGINT    ScreenHeight;
    PEGINT    CenterX;
    PEGINT    CenterY;
    PegPoint  CurPos;
    PegPoint  LastPos;
    PEGUSHORT LastMessage;
    PEGBOOL   PenDown;
    PEGBOOL   SwapAxis;
    PEGBOOL   IsCalibrated;
    PEGBOOL   IsInitialized;
} TOUCH_STATE;

/*--------------------------------------------------------------------------*/
// TOUCH_RAW_POINT
/*--------------------------------------------------------------------------*/
typedef struct
{
    PEGUSHORT RawX;
    PEGUSHORT RawY;
} TOUCH_CALIBRATION_POINT;

/*--------------------------------------------------------------------------*/
// TOUCH_RAW_DATA
//
// Structure for setting the raw calibration data
/*--------------------------------------------------------------------------*/
typedef struct
{
    TOUCH_CALIBRATION_POINT UL;
    TOUCH_CALIBRATION_POINT UR;
    TOUCH_CALIBRATION_POINT LR;
    TOUCH_CALIBRATION_POINT LL;
} TOUCH_RAW_DATA;

/*--------------------------------------------------------------------------*/
// indexes into TOUCH_CAL_DATA
/*--------------------------------------------------------------------------*/

#define TC_UL 0
#define TC_LL 1
#define TC_UR 2
#define TC_LR 3

#if defined(PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION)
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class TouchMesgWin : public PegDialog
{
    public:
        TouchMesgWin(PEGINT iLeft, PEGINT iTop);
        void SetText(char *Mesg);

    private:
        PegTextBox *mpTextBox;
};
#endif


/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
class PegTouchDevice : public PegInputDevice
{
    public:
        PegTouchDevice(void);
        virtual ~PegTouchDevice() {}

       #if defined(PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION)
        virtual PEGBOOL Calibrate(void);
       #endif  // PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION
        virtual PEGBOOL GetLastPos(PegPoint &PutPoint);

        PEGBOOL IsCalibrated(void) const
            { return mState.IsCalibrated; }

        PEGBOOL IsTouched(void) const
            { return mState.PenDown; }

        virtual PEGBOOL Poll(void);
        virtual void Run(void);

        virtual PEGBOOL SetRawCalibrations(TOUCH_RAW_DATA *pRawVals);

    protected:

        virtual PEGBOOL AverageSample(PEGINT &xRaw, PEGINT &yRaw);
        virtual void CheckSendTouchMessage(void);

       #if defined(PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION)
        virtual PegThing  *CreateCalibrationTarget(void);
        virtual TouchMesgWin *CreateCalibrationWin(void);
       #endif

        virtual PEGBOOL DetectTouch(void) = 0;

        virtual PEGINT GetScaledXPos(PEGINT RawVal);
        virtual PEGINT GetScaledYPos(PEGINT RawVal);

        virtual PEGBOOL OneTouchSample(PEGINT &xRaw, PEGINT &yRaw) = 0;

        virtual PegPoint ScaleRawData(PEGINT xRaw, PEGINT yRaw);

       #if defined(PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION)
        virtual void WaitForTouchState(PEGBOOL PenDown);
       #endif  // PEG_TOUCH_SUPPORT_INCLUDE_CALIBRATION

        TOUCH_STATE mState;
};

/*-------------------------------------------------------------------------*/
// this function is declared here and defined in the driver source module
/*-------------------------------------------------------------------------*/
PegTouchDevice *CreatePegTouchDevice(void);

#endif  // PTOUCHDEV_HPP_

#endif  // PEG_TOUCH_SUPPORT

